package com.yunfeng.supercall.fragment;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import se.emilsjolander.stickylistheaders.StickyListHeadersListView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.ly.quickdev.library.utils.JsonUtils;
import com.ly.quickdev.library.view.MySideBar;
import com.ly.quickdev.library.view.MySideBar.OnTouchingLetterChangedListener;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX.Req;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.activity.BaseActivity;
import com.yunfeng.supercall.activity.ChatActivity;
import com.yunfeng.supercall.activity.EnterPriseFriendsActivity;
import com.yunfeng.supercall.activity.FriendsGroupListActivity;
import com.yunfeng.supercall.activity.MyCircleListActivity;
import com.yunfeng.supercall.activity.NewSuperFriendsListActivity;
import com.yunfeng.supercall.adapter.SuperFriendsAdapter;
import com.yunfeng.supercall.bean.AdsBean;
import com.yunfeng.supercall.bean.ContactsBaseBean;
import com.yunfeng.supercall.bean.SuperFriendsBean;
import com.yunfeng.supercall.bean.SystemContactsBean;
import com.yunfeng.supercall.httputils.MyAjaxCallBack;
import com.yunfeng.supercall.manager.AdsManager;
import com.yunfeng.supercall.manager.ManagerFactory;
import com.yunfeng.supercall.manager.ShareManager;
import com.yunfeng.supercall.manager.SuperFriendsManager;

/**
 * 超友
 * 
 * @author 洋
 * 
 */
public class SuperContactsFragment extends BaseFragment implements
		OnItemClickListener {

	StickyListHeadersListView mHeadersListView;
	List<ContactsBaseBean> mContactsBases;
	private MySideBar mSideBar;
	/** 微博微博分享接口实例 */
	SwipeRefreshLayout mSwipeRefreshLayout;
	Activity mActivity;
	
	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.fragment_super_contacts;
	}

	private SuperFriendsAdapter mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mActivity = getActivity();
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		loadNoGroupFriends();
		if (mAdapter != null) {
			mAdapter.notifyDataSetChanged();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		mSideBar = findViewById(R.id.side_bar);
		mSideBar.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() {

			@Override
			public void onTouchingLetterChanged(String s) {
				// TODO Auto-generated method stub
				for (int i = 0, j = mContactsBases.size(); i < j; i++) {
					ContactsBaseBean base = mContactsBases.get(i);
					if (base.getStickyItem().equalsIgnoreCase(s)) {
						mHeadersListView.setSelection(i);
						return;
					}
				}
			}
		});
		super.onViewCreated(view, savedInstanceState);
		mHeadersListView = findViewById(R.id.list);
		mContactsBases = new ArrayList<ContactsBaseBean>();
		mContactsBases.addAll(SystemContactsBean
				.getSystemSuperContactsList(mActivity));
		AdsManager mAdsManager = (AdsManager) ManagerFactory
				.createManager(AdsBean.class);
		mAdsManager.addAdsHeader(mHeadersListView.getWrappedList(), 1);
		mHeadersListView.setAdapter(mAdapter = new SuperFriendsAdapter(
				mActivity, mContactsBases));
		mHeadersListView.setOnItemClickListener(this);

		mSwipeRefreshLayout = findViewById(R.id.swipe);
		mSwipeRefreshLayout.setColorSchemeResources(R.color.main_yellow);
		mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
				loadNoGroupFriends();
			}
		});
	}

	/**
	 * 加载所有未分组的好友
	 */
	public void loadNoGroupFriends() {
		if (mContactsBases.isEmpty()) {
			mSwipeRefreshLayout.setRefreshing(true);
		}
		JSONObject json = BaseActivity.getBaseParams(mActivity);
		try {
			json.put("i_pagenum", 0);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mHttpClient.post(FUNCTION_GET_NO_GROUP_FRIEND_LIST, json,
				new MyAjaxCallBack() {

					@Override
					public void onReceiveData(String data, String msg) {
						// TODO Auto-generated method stub
						Log.i("tag", data);
						List<SuperFriendsBean> mRealFriends = JsonUtils.parseList(
								data, SuperFriendsBean.class);
						Collections.sort(mRealFriends);
						mContactsBases.clear();
						mContactsBases.addAll(SystemContactsBean
								.getSystemSuperContactsList(mActivity));
						mContactsBases.addAll(mRealFriends);
						mAdapter.notifyDataSetChanged();
						mSwipeRefreshLayout.setRefreshing(false);
					}

					@Override
					public void onReceiveError(String msg, String errorCode) {
						// TODO Auto-generated method stub
						mSwipeRefreshLayout.setRefreshing(false);
					}
				});
	}

	/**
	 * 加载所有的好友
	 * @hide
	 */
	@Deprecated
	private void loadSuperFriend() {
		JSONObject json = BaseActivity.getBaseParams(mActivity);
		try {
			json.put("i_pagenum", 0);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mHttpClient.post(FUNCTION_GET_FRIEND_LIST, json, new MyAjaxCallBack() {

			@Override
			public void onReceiveData(String data, String msg) {
				// TODO Auto-generated method stub
				List<SuperFriendsBean> mRealFriends = JsonUtils.parseList(data,
						SuperFriendsBean.class);
				Collections.sort(mRealFriends);
				List<SuperFriendsBean> old = ManagerFactory.createManager(
						SuperFriendsBean.class).getList();
				if (old == null) {
					ManagerFactory.createManager(SuperFriendsBean.class).saveList(
							mRealFriends);
				} else {
					old.clear();
					old.addAll(mRealFriends);
				}
				mContactsBases.clear();
				mContactsBases.addAll(SystemContactsBean
						.getSystemSuperContactsList(mActivity));
				mContactsBases
						.addAll((Collection<? extends ContactsBaseBean>) ManagerFactory
								.createManager(SuperFriendsBean.class).getList());
				mAdapter.notifyDataSetChanged();
				mSwipeRefreshLayout.setRefreshing(false);
			}

			@Override
			public void onReceiveError(String msg, String errorCode) {
				// TODO Auto-generated method stub
				mSwipeRefreshLayout.setRefreshing(false);
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget
	 * .AdapterView, android.view.View, int, long)
	 */
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		ContactsBaseBean base = mContactsBases.get(position
				- mHeadersListView.getHeaderViewsCount());
		if (base instanceof SystemContactsBean) {
			int img = ((SystemContactsBean) base).getImgId();
			if (img == R.drawable.ic_my_circle) {
				startActivity(new Intent(mActivity,
						MyCircleListActivity.class));
			} else if (img == R.drawable.ic_super_friend_group) {
				startActivity(new Intent(mActivity,
						FriendsGroupListActivity.class));
			} else if (img == R.drawable.ic_new_super_friend) {
				startActivity(new Intent(mActivity,
						NewSuperFriendsListActivity.class));
				SuperFriendsManager.setHasNewFriend(mActivity, false);
			} else if (img == R.drawable.ic_weibo_friends) {
				ShareManager.shareToWeiBo(mActivity);
			} else if (img == R.drawable.ic_sms) {
				ShareManager.shareToSms(mActivity);
			} else if (img == R.drawable.ic_wechat_circle) {
				BaseActivity.shareByWeiXin(mActivity, Req.WXSceneTimeline);
			} else if (img == R.drawable.ic_wechat_friends) {
				BaseActivity.shareByWeiXin(mActivity, Req.WXSceneSession);
			} else if (img == R.drawable.ic_qq_friends) {
				ShareManager.shareToQQ(mActivity);

			} else if (img == R.drawable.ic_bussiness_super_friend) {
				startActivity(new Intent(mActivity,
						EnterPriseFriendsActivity.class));
			}
		} else if (base instanceof SuperFriendsBean) {
			startActivity(new Intent(mActivity, ChatActivity.class)
					.putExtra(KEY_DATA, base));
		}
	}
}
